package com.tutu.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tutu.common.util.MD5;
import com.tutu.user.dao.SysUserMapper;
import com.tutu.user.entity.SysUser;
import com.tutu.user.entity.SysUserExample;
import com.tutu.user.entity.SysUserExample.Criteria;
import com.tutu.user.service.ISysUserService;

@Service
public class SysUserServiceImpl implements ISysUserService {
	
	@Resource
	private SysUserMapper sysUserMapper;

	@Override
	public SysUser getUserByName(String loginName) {
		SysUserExample sysUserExample = new SysUserExample();
		sysUserExample.createCriteria().andLoginNameEqualTo(loginName);
		List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
		if(CollectionUtils.isNotEmpty(sysUsers)){
			return sysUsers.get(0);
		}
		return null;
	}

	@Override
	public List<SysUser> getUsers(SysUser sysUser) {
		SysUserExample sysUserExample = new SysUserExample();
		Criteria criteria = sysUserExample.createCriteria();
		if(StringUtils.isNotBlank(sysUser.getAliveFlag())){
			criteria.andAliveFlagEqualTo(sysUser.getAliveFlag());
		}
		sysUserExample.setOrderByClause("CREATE_TIME DESC");
		return sysUserMapper.selectByExample(sysUserExample);
	}

	@Override
	public void saveUser(SysUser sysUser) {
		this.sysUserMapper.insert(sysUser);
	}

	@Override
	public SysUser getSysUserById(String userId) {
		return this.sysUserMapper.selectByPrimaryKey(userId);
	}

	@Override
	public void updateSysUser(SysUser sysUser) {
		this.sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}

	@Override
	public boolean existsLoginName(String loginName) {
		SysUserExample sysUserExample = new SysUserExample();
		Criteria criteria = sysUserExample.createCriteria();
		criteria.andLoginNameEqualTo(loginName);
		int sysUserCount = this.sysUserMapper.countByExample(sysUserExample);
		if(sysUserCount>0)
			return true;
		return false;
	}
	
}
